package com.qf.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qf.entity.Waterfee;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;


@Component
@Data
@ConfigurationProperties(prefix = "alipay")
public class PayUtil {
    //应用私钥:APP_PRIVATE_KEY,
    String APP_PRIVATE_KEY ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCfIjqkHeOUqYDaK/Cebl4dV5tzNxuwljTf7rkJ+1Lu4wi+TI3ezSRnz3dAbvviUD6ds4LjhatYKKaFvoG1HP2iNrTAu0f8zVEekMGNElc8I+bnDeK6csIF+M4d9LFxf+N4c+Tq9PbZtV3PnW6tv9UNd+zbcdaa1t9Q+Z7njmb5K9v8xbs1VyhZ6Bw4xbVt9AHmISk9zNBUy0d07fPtUXlG2rGpy3U2qteGU3t9UxrrYcEz58yrVy4ZAvs14rrBB3zHpq8pDXDoCXOLGnLR//GiAIpp6Knnn+yRAhIZXPCPnCdr7bhn66uF3NjZUHnvrRnjU3Om6w0Rrj9+dVNrCpgZAgMBAAECggEAX6FaTDYKBdS3MmUlfJAmzstPB7YeVchujgQP/wehlx2oq3Ol7japohjzOS15AU/n0S2HguNZTOlg6Q7LM+PwKANoLEpfwHRCQ3QKXJShB+srX77gedts7dnPRAFAgUyxjjlXYDtF/R8Y+SNK1+9oQWfIVzfaF8L5ANiBvfW9vA6+H1zxf9GhUXZFniTNamp6AjGFgDEeQExezd6w7orO/CR5hfyWkM6MBSvftysVJHIXGKT2fq/7+bIiYh0B5ckzCg2r2ZI9FIDNBH6tKc+zlLLQJBRJI6JLbfI6oiZr6YJHpPZU3gkC06OeYHXtOu/ROCAWfGDeeqUbSlLxu229gQKBgQD/POYBGv5HoAsknNkdk4+4EoUjUwue/Sp8L6HMbmqPfEDYJoz687le1i09gWcuohNXu0j3FDqe5IVgTsbTpEinAlQgF3MKiSEvSdfPf6Vg5OnAhBtXGw2fNr2lODWPvShtjq17Jr7PpIMHBI3bjklY7aF7GQjk7bmxsvWPTntmJQKBgQCfm96TzKM7SpWGKqaTsrdAv4OjABV/BbW0RZLiF7VEv2X30CpgLIy26KzJ2LLm7u8STM8zYEK170ozp07WZuDj5NSTE+dRXt57B4PwshuOrpda72S3Q+QbA6Yu3bcEXXZEPkwwBJuqwuNz5B/fAwcCYIxiI4vDztvQnqJEqwuF5QKBgAe+DSwTaYs6q5vctilThJSM20ULC9syysshlsLgxpK7rivgX35tTj42OQEYn8ymVfffUoNMNcsEG5ekTBu3bAzE4VBn5bF4H22Ty9wbfyTFTOHuH/Ywd+VQf8g3QEiv8D4CifimYDtkPiVEWhlC466y5qcZR0/KdkuRpilGtC+RAoGAGptUPlU1vNORG32OqT54l/2VBx4jdzpPcQ7MDo6fNCen6uizt7aVeoRNEZTjNeYMyoMtY0YfuM58EyJH3SvnP3ZNDXua6xCii+XT09MNDvsB1jHkGzzhELPKBzQEp2ChL30nHDYYcR3t5srXmUia6YWIwW9lhC2+rQdUy5er4xUCgYAWT4haJTDTpN35fIJWcuuVsLFj3fKfUh+h4lrQpdqKOLJgarHGr1acFyWfi8ydy6wKSC4vz28cyuIny0yD70ashvneryD+PETGA4WpYGLlny47Ls7VxR60kirInpwaGuOzwWx6jSNunQ68ARay0ZwI8BRGJOFZSlah+CZAxn95BA==";

    //支付宝公钥: ALIPAY_PUBLIC_KEY,
    String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnUa+/9RP2dy4rD/80V43qSvpJxKQFQY1gythJECtV/oMcF8o8C1TSebahUmADZjL+iK92WFnOtIqesXq9hTcilMFheioFuDhNXPC5FpYWmUKaZQfR4Tn1vAi3N0qIVTz2/d7a/JLNiSCprPaCg5nyeWXMlkK+hJubv3sXuzRZHKIkt0lwa48y5qQf2iztZai0WIPxpGht39w0EI7zV0f3Wqs0MythO6DInGsZVS+Rtq+95OVTtmycTNhK0h24v5yjt56HqqHf1F7QYIM8os9SIvTed9VKq5VcI6SGuv/lpDQpzG3M+xJoldwdJla0RGsz5D8NCMgN5Sa9aUUVr1dSQIDAQAB";

    String RETURN_URL;

    String natapp_url;

    String APP_ID;

    public String alipay (HttpServletRequest httpRequest,
                          HttpServletResponse httpResponse, Double fee,int id)   throws ServletException, IOException {
        //请求的支付宝网关地址:这是正式开发网关地址"https://openapi.alipay.com/gateway.do" ,
        //沙箱支付网关地址:"https://openapi.alipaydev.com/gateway.do"
        //应用id:APP_ID,

        //应用私钥:APP_PRIVATE_KEY,
//        String APP_PRIVATE_KEY ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCcnlQF1Y1v3WpxOLsk3mHULsOugMFDsRTM5Cg4lpqoleV6UcQFPDB6YciWThktpV8Bf+LQVRkLEIWL+nBY2u81F2FKM7CUeAsMToHTg+2DykowgbB425kJEyHS/WPBJ/5MYAHeLvw66i5AMrPVRd9aAhW0poRI5wa+qqbfFWnh139whAX687POPUkGDr1DFSjn3Kp7RfRhivP981eUxkIXScaGit6/sqw0V7NzeuADiIzhOThVE74spPWFTvTghL2mCHe/Z15SPrZjYyati6bEzvIB+zE918ttQLzYBax7E16z47SfVQ56YGWY/nTqaqYC5I+d/76m+hdOuV18GSDHAgMBAAECggEABev+pVHTzT0D/0/z04sNC+bQw1Je/F4/uRePZcRcCK/SQ3PAA6qo8per6/L8BLvSAc6CFvqAvehcBFTt1o5AqTM/HH0j2hlH4ak+Q28jle4aLAdbUtaKzMsKZl3qXa1TcjPXHj2Mj/W21fqUziWcMl0N0QzSx6bknGT7dkfq/v0FiFE8fIPMdaC7mIwdaeVJBh5JZ37G6RKIp8eC3JNaUesWtK55Ln7RP8NKXH/B7gHg28IxJtGGpdD+uzMHRxcCcn9CLrb78XwGWZrPbS7o0i61oUWx7JQtgJnfmYIWm72oxsPGFC++571Fq9P/PT+gzzZ80hvEb/ooK87qQwp/IQKBgQDzThAvdBB7LePqwuwUwVzVQj1Br0F+UCPaozrwcKy3QvOYzx18eJibXd2yjIfI9jz3y/YWH8jBRdnCOD3gECJD9FVbiWxrtQXdipW5TTdQ16roKF992OjyfbLFDthvUnnB3YYvkmy0NbjmIQb2QENhJKoUiPXX4b/U9+7vsFhMyQKBgQCkylqMK4hqRiPvLQ6ey2kY0oeylQnKlyRjtGAcVhRIyXJrucjuDubZ0UFglpATW2qxOHxF2UceB0UMb82sqos6G0/c/QorYsCcwl4o/H+Et0MM84FWLmAQ2U3lQQwq/mwaat3y+bXTMWujuEJjDkYMlL62htMXWiNPJygFC4UZDwKBgQDS2qg0N8XAurh2z46BPkIRn2Iu9o8vxIwOAsAld8B24dLybywCnxDCNMrY3ekfRCb6KQyPh+QXe02If+0/rTvE4I7I0YPgo/T9VgkDmeOCcCVxJOv8yGq5HC49kOwJLdTPKggMWj1Sv+wJpD3AU1kRaxDq0vcbrEzvaB7uIkmj4QKBgEKWifQt+xeGdCrHeCruYu9m0vJ0itpULJ4lWtjYZ6Xp0NR65Bhd6SaHCfXvoRRRUlF+U6kd3UDjCyG1Y9SrrUqzkpfnn95cLxyo5ceGEDkenHd1RgP/BU6JoJJMsJAG8sPR/xGZmcFXLPmhESJZuDCJQm8bF91NzQrE9cTkHMo9AoGBAIxJoaPPdltQs/REr0i7lGK9s1fZpdnEZDpoJ6uWHXqD3odxqQEeU9YqcMxGqbF6UINXk+RbR3cqBClOF7KAWix6riF5sBtdAIuTPUaIePERVkzWbfSa7sJCAqwvpQODvt3sPdWqJ/SRXAvSkdya9kVf+qw4FlaP4BOIu9Mci0kp";
        //数据格式:目前只能是json:   FORMAT,
        //编码方式:--utf-8-- CHARSET,

        // 生成签名的方式:--java推荐RSA2--SIGN_TYPE

        //通过"支付宝开发助手"生成个人应用私钥及个人应用公钥  再在"支付宝控制台--沙箱应用--自定义密钥"生成支付宝公钥

        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , APP_ID, APP_PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request



        //设置请求回调地址,即访问支付页面之后回来的位置
        //使用花生壳/natapp等方式皆可生成内网穿透地址
        alipayRequest.setReturnUrl(RETURN_URL);//  同步回调--自动发送get请求
        //System.out.println("++++++++++"+RETURN_URL);
        //System.out.println("++++++++++"+httpRequest.getRequestURI());
        //String notifyUrl = "http://ep3bv7.natappfree.cc"+ httpRequest.getRequestURI()+"/notify";
        //System.out.println("notifyUrl:=======>"+notifyUrl);
        //http://localhost:7777/property/pay/notify
        alipayRequest.setNotifyUrl("http://47.94.195.189:7777"+httpRequest.getRequestURI()+"/notify"); //异步回调--自动发送post请求 --在公共参数中设置回跳和通知地址
                                  //http://47.94.195.189:7777/property/pay/notify

        String userId = URLEncoder.encode("33");
        alipayRequest.setBizContent( "{"  +
                "\"out_trade_no\":\""+ UUID.randomUUID().toString().replaceAll("-","") +"\","  +  //订单编号
                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +       //与支付宝签约的商品号
                "\"total_amount\":"+fee+","  +                            //总价
                "\"subject\":\""+fee+"缴费金额\","  +                       //产品名
                "\"body\":\""+fee+"缴费金额\","  +                          //产品体  可以和产品名一样
                "\"passback_params\":\""+id+"\""  +
                "}" ); //填充业务参数
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
            String get = alipayClient.pageExecute(alipayRequest, "GET").getBody();
            System.out.println(get);
//            System.out.println(form+"=================================");
            return form;
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
//        httpResponse.setContentType( "text/html;charset="  + "utf-8");
//        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
//        httpResponse.getWriter().flush();
//        httpResponse.getWriter().close();
        return null;
    }

}
